    //
//  main.m
//  07 - MRCProvince
//
//  Created by 韩旭 on 16/7/12.
//  Copyright © 2016年 基础. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "City.h"
#import "Province.h"
#import "Zone.h"
#import "TempZone.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
#pragma mark - 作业1
#if 1
        {
            NSString *path = @"/Users/hanxu/DLS160710/Objective-C/ClassCode/07 - MRCProvince/07 - MRCProvince/Homework-05 2/area.txt";
            NSString *stringPath = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
            NSMutableArray *arrAll = [NSMutableArray array];
            NSArray *arrString = [stringPath componentsSeparatedByString:@"\n"];
            for (NSString *string in arrString) {
                if (![string hasPrefix:@" "]) {
                    Province *pro = [Province provinceWithName:string];
                    [arrAll addObject:pro];
                } else if ([string hasPrefix:@"  "] && ![string hasPrefix:@"    "]) {
                    City *city = [City provinceWithName:string];
                    Province *pro = [arrAll lastObject];
                    [pro.arrCity addObject:city];
                } else {
                    City *city = [[[arrAll lastObject] arrCity ] lastObject];
                    [city.arrCity addObject:[Zone provinceWithName:string]];
                }
            }
            NSLog(@"%@", arrAll);
        }
#endif
#pragma mark - 第五题
#if 1
        {
            NSString *path = @"/Users/hanxu/DLS160710/Objective-C/ClassCode/07 - MRCProvince/07 - MRCProvince/Homework-05 2/Location1.txt";
            NSString *string = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
            NSArray *arr = [string componentsSeparatedByString:@","];
            NSMutableArray *arrAll = [NSMutableArray array];
            for (NSString *arrString in arr) {
                NSArray *arrStr = [arrString componentsSeparatedByString:@"|"];
                TempZone *temp = [[TempZone alloc] initWithName:[arrStr firstObject] cityNumber:[arrStr lastObject]];
                [arrAll addObject:temp];
            }
            NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
            [arrAll sortUsingDescriptors:@[sort]];
            NSMutableArray *finalArray = [NSMutableArray array];
            for (TempZone *temp in arrAll) {
                if ([temp.cityNumber hasSuffix:@"0000"]) {
                    Province *pro = [Province zoneWithTempCity:temp];
                    [finalArray addObject:pro];
                } else if ([temp.cityNumber hasSuffix:@"00"] && ![temp.cityNumber hasSuffix:@"0000"]) {
                    Province *pro = [finalArray lastObject];
                    City *city = [City zoneWithTempCity:temp];
                    [pro.arrCity addObject:city];
                } else {
                    Province *pro = [finalArray lastObject];
                    City *city = [pro.arrCity lastObject];
                    Zone *zone = [Zone zoneWithTempCity:temp];
                    [city.arrCity addObject:zone];
                }
                
            }
            NSLog(@"%@", finalArray);
        }
#endif
        
        
        
    }
    return 0;
}
